package com.even.camerarecoder

import android.media.MediaCodecInfo
import android.media.MediaCodecList
import android.media.MediaFormat
import com.evendai.loglibrary.Timber

object H264Util {

    /** 打印支持的H264编码器 */
    @Suppress("DEPRECATION")
    fun printSupportedH264Encoder() {
        val codecCount = MediaCodecList.getCodecCount()
        for (i in 0 until codecCount) {
            val codecInfo = MediaCodecList.getCodecInfoAt(i)
            if (!codecInfo.isEncoder) continue              // 如果不是编码器，则找下一个
            val mimeType = MediaFormat.MIMETYPE_VIDEO_AVC   // H264的mime类型
            val supportedH264 = codecInfo.supportedTypes.any { type -> type.equals(mimeType, true) }
            if (supportedH264) {
                val colorFormats = codecInfo.getCapabilitiesForType(mimeType).colorFormats
                // 通过int值，找到它是在MediaCodecInfo.CodecCapabilities中的哪个变量
                val colorFormatsConvert = Array(colorFormats.size) { index ->
                    ReflectUtil.getPublicStaticIntFieldNameByValue(MediaCodecInfo.CodecCapabilities::class.java, colorFormats[index])
                }
                Timber.i("H264编码器：${codecInfo.name}, 支持的颜色格式：${colorFormatsConvert.contentToString()}")
            }
        }
    }

    /** 打印支持的H264编码器 */
    @Suppress("DEPRECATION")
    fun printSupportedAACEncoder() {
        val codecCount = MediaCodecList.getCodecCount()
        for (i in 0 until codecCount) {
            val codecInfo = MediaCodecList.getCodecInfoAt(i)
            if (!codecInfo.isEncoder) continue              // 如果不是编码器，则找下一个
            val mimeType = MediaFormat.MIMETYPE_AUDIO_AAC   // H264的mime类型
            val supported = codecInfo.supportedTypes.any { type -> type.equals(mimeType, true) }
            if (supported) {
                Timber.i("AAC编码器：${codecInfo.name}")
            }
        }
    }
}